{extend name="adminblue/base" /}
{block name="resources"/}
<style>
.label-inline-block{display: inline-block;margin:0 15px 0 0;vertical-align: middle;}
.set-style dl dd.goods-list{margin:0;line-height: normal;width: 100%;}
.combo-package-mask-layer{position: fixed;width:100%;background: rgba(0,0,0,0.7);height:100%;left:0;z-index:3000;top:0;display: none;}
.goods-list-mask-layer{position: fixed;width: 800px;top: 20%;z-index: 3000;left: 50%;background: #ffffff;padding:10px;border-radius: 5px;margin-left:-410px;display: none;}
.goods-list-mask-layer table{width:100%;}
.table-class tr th{padding:5px;}
.table-class tr td{text-align: center;}
.ns-main>#turn-ul{display:none !important;}
.ini{color: #777;margin-left: 10px;font-size: 12px;}
i.fa-times:hover{cursor: pointer;}
</style>
{/block}
{block name="alert_info"}
<p>只有实物商品才能参与组合套餐活动！</p>
<p>商品原价与节省价仅作为参考，不参与实际计算！</p>
<p>商品价格取得是商品sku价格中的最低价格！参与套餐售卖的的商品sku价格不要相差太大。</p>
<p style="color: red;margin:0;">注：在组合商品发布之后，切勿修改商品价格。</p>
{/block}
{block name="main"}
<div class="set-style">
	<dl>
		<dt>组合套餐名称：</dt>
		<dd><input type="text" id="combo_package_name" class="input-common" maxlength="90" value="{$info.combo_package_name}">
			<p class="error">请输入组和套餐名称</p>
		</dd>
	</dl>
	<dl>
		<dt>组合套餐价格：</dt>
		<dd><input type="number" id="combo_package_price" class="input-common" value="{$info.combo_package_price}">
			<p class="error">请输入组和套餐价格</p>
		</dd>
	</dl>
	<dl>
		<dt>原价：</dt>
		<dd><span id="original_price">{if condition="$info['original_price'] > 0"}{$info.original_price}{else /}0.00{/if}</span>元</dd>
	</dl>
	<dl>
		<dt>节省价：</dt>
		<dd><span id="save_the_price">{if condition="$info['save_the_price'] > 0"}{$info.save_the_price}{else /}0.00{/if}</span>元</dd>
	</dl>
	<dl>
		<dt>是否上架：</dt>
		<dd>
			{if condition="$id > 0"}
			<input type="radio" name="status" class="input-common" {if condition="$info['is_shelves'] eq 1"}checked="checked"{/if} id="shelves_up" value="1"/>
			<label class="label-inline-block" for="shelves_up" >上架</label>
			<input type="radio" name="status" class="input-common" id="shelver_down" value="0"  {if condition="$info['is_shelves'] eq 0"}checked="checked"{/if}/>
			<label class="label-inline-block" for="shelver_down">下架</label>
			{else/}
			<input type="radio" name="status" class="input-common" checked="checked" id="shelves_up" value="1"/>
			<label class="label-inline-block" for="shelves_up" >上架</label>
			<input type="radio" name="status" class="input-common" id="shelver_down" value="0" />
			<label class="label-inline-block" for="shelver_down">下架</label>
			{/if}
		</dd>
	</dl>
	<dl>
		<dt>选择商品：</dt>
		<dd>
			<button class="btn-common js-select-goods">选择商品</button><span class="ini">组和套餐商品不能少于两种不能多于六种</span>
			<p class="error">组和套餐商品不能少于两种</p>
		</dd>
	</dl>
	<dl>
		<dd class="goods-list">
			<table class="table-class">
				<colgroup>
					<col width="50%">
					<col width="20%">
					<col width="15%">
					<col width="15%">
				</colgroup>
				<thead>
					<tr>
						<th>商品名称</th>
						<th>价格</th>
						<th>库存</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
		</dd>
	</dl>
	<dl>
		<dt></dt>
		<dd>
			<button class="btn-common btn-big" onclick="save();">提交</button>
			<button class="btn-common btn-big" onclick="click_reset();">重置</button>
		</dd>
	</dl>
</div>
<div class="combo-package-mask-layer"></div>
<div class="goods-list-mask-layer">
	<table class="mytable">
		<tr>
			<th>
				商品名称：<input id="goods_name" class="input-medium input-common" type="text" placeholder="要搜索的商品名称">
				<input type="button" onclick="LoadingInfo(1)" value="搜索" class="btn-common">
			</th>
		</tr>
	</table>
	<table class="table-class">
		<colgroup>
			<col width="5%">
			<col width="60%">
			<col width="15%">
			<col width="15%">
			
		</colgroup>
		<thead>
			<tr>
				<th><input type="checkbox" name="select" onclick="CheckAll(this)" /></th>
				<th style="text-align: left;">商品名称</th>
				<th>价格</th>
				<th>库存</th>
			</tr>
		</thead>
		<tbody></tbody>
	</table>
	<button class="btn-common" style="margin: 20px auto 10px auto !important;display: block;" id="determine">确定</button>
</div>
<input type="hidden" id="goods_id_array" value="{$info.goods_id_array}">
<script>
var BatchSend = new Array();
$(function(){
	if($("#goods_id_array").val().length > 0){
		BatchSend = $("#goods_id_array").val().split(",");
	}
	refreshSelectGoods();
});

$(".js-select-goods").click(function(){
	LoadingInfo(1);
	modal("show");
});

//点击遮罩隐藏商品选择框
$(".combo-package-mask-layer").click(function(){
	modal("hide");
})

//全选
function CheckAll(obj){
	var checked = obj.checked;
	$(".table-class tbody tr input:checkbox").prop("checked",checked);
}

function LoadingInfo(page_index) {
	var goods_name =$("#goods_name").val();
	var goods_id_array = $("#goods_id_array").val();
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/goods/getSelectGoodslist')}",
		data : {
			"page_index" : page_index,
			"page_size" : $("#showNumber").val(),
			"goods_name":goods_name,
			"goods_id_array" : goods_id_array,
			"type" : "select"
		},
		success : function(data) {
			var html = '';
			if (data["data"].length > 0) {
				for (var i = 0; i < data["data"].length; i++) {

					html += '<tr>';
						html += '<td>';
								html += '<input value="' + data["data"][i]["goods_id"] + '" name="sub" data-state="'+data["data"][i]["state"]+'" type="checkbox">';
						html += '</td>';

						html += '<td style="text-align:left;">' + data["data"][i]["goods_name"] + '</td>';

						html += '<td>' + data["data"][i]["price"] + '</td>';
						
						html += '<td>' + data["data"][i]["stock"]  + '</td>';

					html += '</tr>';
				}
			} else {
				html += '<tr align="center"><td colspan="9" style="text-align: center;font-weight: normal;color: #999;">暂无符合条件的数据记录</td></tr>';
			}
			$(".goods-list-mask-layer .table-class tbody").html(html);
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
			if($(".goods-list-mask-layer #turn-ul").length == 0){
				$(".goods-list-mask-layer").append($("#turn-ul").clone(true).show());
			}
			$(".ns-main>#turn-ul").remove();
		}
	});
}
//点击确定
$("#determine").click(function(){
	$("input[name='sub']:checked").each(function () {
		BatchSend.push($(this).val());
	});
	if(BatchSend.length > 6){
		showTip("所选商品不能多于六种","warning");
		return false;
	}
	$("#goods_id_array").val(BatchSend.toString());
	refreshSelectGoods();
	modal("hide");
})
//模态框隐藏显示
function modal(type){
	if(type == "show"){
		$(".combo-package-mask-layer").show();
		$(".goods-list-mask-layer").show();
	}else if(type == "hide"){
		$(".combo-package-mask-layer").hide();
		$(".goods-list-mask-layer").hide();
	}
}

//商品取消选择
function cancelSelect(goods_id){
	for(var i = 0; i < BatchSend.length; i++){
		if(BatchSend[i] == goods_id){
			BatchSend.splice(i,1);
		}
	}
  	$("#goods_id_array").val(BatchSend.toString());
	refreshSelectGoods();
}

//刷新选择的商品
function refreshSelectGoods(){
	var goods_id_array = $("#goods_id_array").val();
	if(goods_id_array.length > 0){
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/goods/getSelectGoodslist')}",
			data : {
				"goods_id_array" : goods_id_array,
				"type" : "selected"
			},
			async : false,
			success : function(data) {
				var html = '';
				if (data["data"].length > 0) {
					for (var i = 0; i < data["data"].length; i++) {

						html += '<tr>';
							html += '<input value="' + data["data"][i]["goods_id"] + '"  type="hidden">';

							html += '<td style="text-align:left;">' + data["data"][i]["goods_name"] + '</td>';

							html += '<td class="goods_price">' + data["data"][i]["price"] + '</td>';
							
							html += '<td>' + data["data"][i]["stock"]  + '</td>';

							html += '<td><label for=""><i class="fa fa-times" aria-hidden="true fa-2x" onclick="cancelSelect(' + data["data"][i]["goods_id"] + ')"></i></label></td>';

						html += '</tr>';
					}
				} 
				$(".goods-list .table-class tbody").html(html);
			}
		})
	}else{
		$(".goods-list .table-class tbody").html("");
	}
	calculate_original_price();//计算原价
}

//价格
$("#combo_package_price").change(function(){
	var price = parseFloat(parseFloat($(this).val()).toFixed(2));
	var original_price = parseFloat(parseFloat($("#original_price").text()).toFixed(2)); //原价
	if(price < 0){ 
		price = 0; 
	}else if(price > original_price){
		price = original_price;
	}
	$(this).val(price);
	calculate_original_price();
})

//重置
function click_reset(){
	BatchSend = Array();
	$("#goods_id_array").val("");
	$(".goods-list .table-class tbody").html("");
}

var is_click = false;
function save(){
	var combo_package_name = $("#combo_package_name").val();
	var combo_package_price = $("#combo_package_price").val();
	var goods_id_array = $("#goods_id_array").val();
	var is_shelves = $("[name='status']:checked").val();
	var original_price = $("#original_price").text();
	var save_the_price = $("#save_the_price").text();
	if(vertify(combo_package_name, combo_package_price)){
		if(is_click){return false;}
		is_click = true;
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/promotion/addOrEditComboPackage')}",
			data : {
				"goods_id_array" : goods_id_array,
				"combo_package_name" : combo_package_name,
				"combo_package_price" : combo_package_price,
				"is_shelves" : is_shelves,
				"id" : "{$id}",
				"original_price" : original_price,
				"save_the_price" : save_the_price
			},
			success : function(data) {
				if (data["code"] > 0) {
					is_click = false;
					showMessage("success","保存成功","{:__URL('ADMIN_MAIN/promotion/comboPackagePromotionList')}")
				}else{
					showMessage("error", data["message"]);
				}
			}
		})
	}
}
//验证
function vertify(combo_package_name, combo_package_price){
	if(combo_package_name.length == 0){
		$("#combo_package_name").next(".error").show();
		$("#combo_package_name").focus();
		return false;
	}else{
		$(".error").hide();
	}
	if(BatchSend.length < 2){
		$(".js-select-goods").next().next(".error").show();
		return false;
	}else{
		$(".error").hide();
	}
	if(combo_package_price.length == 0){
		$("#combo_package_price").next(".error").show();
		$("#combo_package_price").focus();
		return false;
	}else{
		$(".error").hide();
	}
	return true;
}

function calculate_original_price(){
	var original_price = 0;
	$(".goods-list .table-class tbody tr td.goods_price").each(function(i,e){
		var price = parseFloat($(e).text());
		original_price += price;
	})
	var combo_package_price = parseFloat($("#combo_package_price").val());
	$("#original_price").text(original_price.toFixed(2)); //原价
	if(combo_package_price > 0){
		if(combo_package_price > original_price){
			combo_package_price = original_price;
			$("#combo_package_price").val(combo_package_price);
		}
		$("#save_the_price").text((original_price - combo_package_price).toFixed(2)); //节省价
	}
}
</script>
{/block}